package com.xuchongyang.easylinphone;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.SurfaceView;
import android.content.res.Resources;
import com.xuchongyang.easyphone.EasyLinphone;
import android.view.View;

public class VideoActivity extends Activity implements View.OnClickListener {

    private Resources R;
    private String packageName;

    private SurfaceView mRenderingView;
    private SurfaceView mPreviewView;


//    @BindView(R.id.video_rendering) SurfaceView mRenderingView;
//    @BindView(R.id.video_preview) SurfaceView mPreviewView;

    private FinishVideoActivityReceiver mReceiver;
    public static final String RECEIVE_FINISH_VIDEO_ACTIVITY = "receive_finish_video_activity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        R = this.getResources();
        packageName = this.getPackageName();

        setContentView(R.getIdentifier("activity_video", "layout", packageName));

        mRenderingView = (SurfaceView) findViewById(R.getIdentifier("video_rendering", "id", packageName));
        mPreviewView = (SurfaceView) findViewById(R.getIdentifier("video_preview", "id", packageName));

        IntentFilter intentFilter = new IntentFilter(RECEIVE_FINISH_VIDEO_ACTIVITY);
        mReceiver = new FinishVideoActivityReceiver();
        registerReceiver(mReceiver, intentFilter);
        EasyLinphone.setAndroidVideoWindow(new SurfaceView[]{mRenderingView}, new SurfaceView[]{mPreviewView});
    }

    @Override
    protected void onResume() {
        super.onResume();
        EasyLinphone.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        EasyLinphone.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mReceiver != null) {
            unregisterReceiver(mReceiver);
        }
        EasyLinphone.onDestroy();
    }

    //    @OnClick(R.id.video_hang)
    public void hang() {
        EasyLinphone.hangUp();
        finish();
    }

    //    @OnClick(R.id.video_mute)
    public void mute() {
        EasyLinphone.toggleMicro(!EasyLinphone.getLC().isMicMuted());
    }

    //    @OnClick(R.id.video_speaker)
    public void speaker() {
        EasyLinphone.toggleSpeaker(!EasyLinphone.getLC().isSpeakerEnabled());
    }

    @Override
    public void onClick(View view) {
        if(R.getIdentifier("video_hang", "id", packageName) == view.getId()){
            hang();
        }
        if(R.getIdentifier("video_mute", "id", packageName) == view.getId()){
            mute();
        }
        if(R.getIdentifier("video_speaker", "id", packageName) == view.getId()){
            speaker();
        }
    }

    public class FinishVideoActivityReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            VideoActivity.this.finish();
        }
    }
}
